package com.quanshu.exchange.ui.gui.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.db.UserDao;
import com.quanshu.exchange.support.entity.NotifyMsgEntity;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 通知
 * Created by jye on 2017/12/14.
 */

public class NotifyMessageListActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private SingleAdapter<NotifyMsgEntity> adapter;
    private List<NotifyMsgEntity> notifyMessageList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notify_message_list);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initView() {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleAdapter<>(new DataDisplayer<NotifyMsgEntity>(this, R.layout.item_notify_message) {
            @Override
            public void bindData(BaseViewHolder holder, LayoutWrapper<NotifyMsgEntity> wrapper, final NotifyMsgEntity item, int position) {
                holder.setRoundImageUrl(R.id.notify_message_head_portrait, item.getAvatar(), R.drawable.default_avatar);
                holder.setText(R.id.notify_message_text_name, item.getNickname());
                holder.setText(R.id.notify_message_text_message, item.getApply_content());
                if (item.getApply_status() == 0) {
                    holder.setText(R.id.agree_apply, "同意");
                    holder.getView(R.id.agree_apply).setEnabled(true);
                } else if (item.getApply_status() == 1) {
                    holder.setText(R.id.agree_apply, "已同意");
                    holder.getView(R.id.agree_apply).setEnabled(false);
                } else if (item.getApply_status() == 2) {
                    holder.setText(R.id.agree_apply, "已拒绝");
                    holder.getView(R.id.agree_apply).setEnabled(false);
                } else {
                    holder.setText(R.id.agree_apply, "");
                    holder.getView(R.id.agree_apply).setEnabled(false);
                }
                holder.setOnClickListener(view -> {
                    if (item.getApply_status() == 0) {
                        Intent intent = new Intent(NotifyMessageListActivity.this, NotifyDetailsActivity.class);
                        intent.putExtra("NotifyMessage", item);
                        startActivity(intent);
                    }
                });
                holder.setOnClickListener(R.id.agree_apply_rel, view -> {
                    if (item.getApply_type() == 1) {
                        doAcceptFriendApply(item);
                        holder.setText(R.id.agree_apply, "已同意");
                        holder.getView(R.id.agree_apply).setEnabled(false);
                    } else if (item.getApply_type() == 2) {
                        doJoinMember2Group(item);
                        holder.setText(R.id.agree_apply, "已同意");
                        holder.getView(R.id.agree_apply).setEnabled(false);
                    }
                });
            }
        });
        recyclerView.setAdapter(adapter);
    }

    /**
     * 同意好友邀请
     */
    private void doAcceptFriendApply(NotifyMsgEntity item) {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .addFriend(AppUtils.getSignParams(currentUser.getMemberid(), item.getApply_member()), currentUser.getMemberid(), item.getApply_member())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<UserEntity>>() {
                    @Override
                    public void onSuccess(BaseResponse<UserEntity> response) {
                        UserEntity userEntity = response.getInfo();
                        if (userEntity != null) {
                            UserDao.getInstance().save(userEntity);
                        }
                    }
                });
    }

    private void doJoinMember2Group(NotifyMsgEntity item) {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .joinGroup(AppUtils.getSignParams(item.getApply_member(), item.getHx_groupid()), item.getApply_member(), item.getHx_groupid())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                    }
                });
    }

    private void initData() {
        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .getNotifies(AppUtils.getSignParams(currentUser.getMemberid()), currentUser.getMemberid())
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse<List<NotifyMsgEntity>>>() {
                    @Override
                    public void onSuccess(BaseResponse<List<NotifyMsgEntity>> response) {
                        notifyMessageList = response.getInfo();
                        if (notifyMessageList != null && notifyMessageList.size() > 0) {
                            adapter.setData(notifyMessageList);
                        }
                    }
                });

    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
    }
}
